package com.controll;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dao.UsersDao;
import com.pojo.Users;
import com.service.User_logService;
import com.service.UsersService;
import com.util.MybatisSessionFactory;

@Controller
public class UsersControll {
	/**
	 * 调用service层方法
	 */
	UsersService service = new UsersService();
    User_logService service_log=new User_logService();
	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	@ResponseBody
	/**
	 * 登录判断
	 * @param account
	 * @param password
	 * @param response
	 * @return
	 */
	public String query_login(
			@RequestParam(value = "account", defaultValue = "") String account,
			@RequestParam(value = "password", defaultValue = "") String password,
			HttpServletResponse response) {
		//System.out.println(account +""+ password);
		Users result = service.query_login(account, password);
		
		//System.out.println(result);
		if (!"1".equals(result.getCounter())) {
			return "ERR";
		}
		response.addCookie(new Cookie("User", result.getId()));
		service_log.add_log("0",result.getId(),"登录");
		return "OK";
	}
	@RequestMapping(value = "/queryuser.action", method = RequestMethod.GET)
	@ResponseBody
	public Map queryuser(
			@CookieValue(value = "User", defaultValue = "") String userId,
			HttpServletResponse response) {
		//System.out.println(userId);
		if ("".equals(userId)) {
			try {
				response.sendRedirect("login.html");
				return null;
			} catch (Exception e) {
				System.out.println("[error] queryUser.action:redirect\r\n");
				return null;
			}
		}
	
	     Map map=service.queryuser(userId) ;
		   return map;
	}
}
